package com.ruoyi.wechat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.gaode.GaoDeUtils;
import com.ruoyi.common.utils.gaode.domain.GaoDeDto;
import com.ruoyi.wechat.domain.BackTextInfo;
import com.ruoyi.wechat.domain.WeChatUser;
import com.ruoyi.wechat.domain.WeChatXmlData;
import com.ruoyi.wechat.mapper.WeChatMapper;
import com.ruoyi.wechat.service.ReceiveWechatService;
import com.ruoyi.wechat.service.SendTemplateMsgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import static com.ruoyi.wechat.constants.WeChatConstants.*;

/**
 * @author zyj
 * @date 2022-08-22 15:19
 */
@Service
public class ReceiveWechatServiceImpl implements ReceiveWechatService {

    @Autowired
    private WeChatMapper weChatMapper;


    @Autowired
    private SendTemplateMsgService sendTemplateMsgService;


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 处理微信消息
     */
    @Override
    public BackTextInfo execWechatXmlData(WeChatXmlData weChatXmlData) {
        WeChatUser weChatUser = new WeChatUser();
        logger.debug("收到信息:{},报文----{}", weChatXmlData.getMsgType(), JSON.toJSONString(weChatXmlData));
        weChatUser.setOpenId(weChatXmlData.getFromUserName());
        if (weChatMapper.selectCountByOpenId(weChatUser) == 0) {
            weChatMapper.insertWechatUser(weChatUser);
        }
        String returnContent = "设置成功";
        // 定位信息
        if (weChatXmlData.getMsgType().equals(EVEN) && weChatXmlData.getEvent().equals(EVEN_LOCATION)) {
            weChatUser.setLatitude(weChatXmlData.getLatitude());
            weChatUser.setLongitude(weChatXmlData.getLongitude());
            // 调用高德地图接口 获取所在区县
            GaoDeDto gaoDeDto = GaoDeUtils.getAddress(weChatXmlData.getLatitude(), weChatXmlData.getLongitude());
            weChatUser.setAddress(gaoDeDto.getRegeocode().getFormatted_address());
            weChatUser.setProvince(gaoDeDto.getRegeocode().getAddressComponent().getProvince());
            weChatUser.setCity("[]".equals(gaoDeDto.getRegeocode().getAddressComponent().getCity()) ? gaoDeDto.getRegeocode().getAddressComponent().getDistrict() : gaoDeDto.getRegeocode().getAddressComponent().getCity());
            weChatUser.setDistrict(gaoDeDto.getRegeocode().getAddressComponent().getDistrict());
            weChatUser.setTownship(gaoDeDto.getRegeocode().getAddressComponent().getTownship());
            weChatMapper.updateUserAddressByOpenId(weChatUser);
            return null;
        } else if (weChatXmlData.getMsgType().equals(TEXT)) {
            String content = weChatXmlData.getContent();
            if ("菜单".equals(content)) {
//                returnContent = "设置昵称 和 生日请回复 \n" +
//                        "如: 张三:19970207 \n" +
//                        "回复 \n" +
//                        " 0: 自动发送(每天早上9点)\n" +
//                        " 1: 立即发送 \n" +
//                        " 2: 取消自动发送";
                returnContent = "给你讲个故事 \n" +
                        "我长话短说 \n" +
                        "我喜欢你/:jump \n" +
                        "感谢关注哦/:heart \n" +
                        "回复 '菜单' 查看更多选项,\n" +
                        "请设置允许获取所在位置哦,不然没有天气信息.\n"+
                        "设置昵称 和 生日请回复 \n" +
                        "如: 张三:19970207 \n" +
                        "回复 \n" +
                        " 0: 自动发送(每天早上9点)\n" +
                        " 1: 立即发送 \n" +
                        " 2: 取消自动发送";;
            } else if (content.contains(":")) {
                // 文本消息
                String[] contextSpilt = content.split(":");
                weChatUser.setNickName(contextSpilt[0]);
                weChatUser.setBirthday(DateUtils.parseDate(contextSpilt[1]));
                weChatMapper.updateUserInfoByOpenId(weChatUser);
                returnContent = "我亲爱的" + contextSpilt[0] + "生日"+contextSpilt[1]+",设置成功";
            }else if (content.contains("：")) {
                // 文本消息
                String[] contextSpilt = content.split("：");
                weChatUser.setNickName(contextSpilt[0]);
                weChatUser.setBirthday(DateUtils.parseDate(contextSpilt[1]));
                weChatMapper.updateUserInfoByOpenId(weChatUser);
                returnContent = "我亲爱的" + contextSpilt[0] + "生日"+contextSpilt[1]+",设置成功";
            } else if (content.contains("1")) {
                sendTemplateMsgService.sendTemplateMsg(weChatMapper.selectWechatUser(weChatUser));
                return null;
            } else if (content.contains("0")) {
                weChatUser.setAuto(true);
                weChatMapper.updateUserAutoByOpenId(weChatUser);
                returnContent = "自动发送设置成功!";
            } else if (content.contains("2")) {
                weChatUser.setAuto(false);
                weChatMapper.updateUserAutoByOpenId(weChatUser);
                returnContent = "取消自动发送设置成功!";
            } else {
                returnContent = "回复 '菜单' 查看更多选项";
            }
        } else if (weChatXmlData.getEvent().equals(SUBSCRIBE)) {
            // 订阅事件
            returnContent = "给你讲个故事 \n" +
                    "我长话短说 \n" +
                    "我喜欢你/:jump \n" +
                    "感谢关注哦/:heart \n" +
                    "回复 '菜单' 查看更多选项,\n" +
                    "请设置允许获取所在位置哦,不然没有天气信息.\n"+
                    "设置昵称 和 生日请回复 \n" +
                    "如: 张三:19970207 \n" +
                    "回复 \n" +
                    " 0: 自动发送(每天早上9点)\n" +
                    " 1: 立即发送 \n" +
                    " 2: 取消自动发送";;
        }
        return new BackTextInfo(weChatXmlData.getFromUserName(), weChatXmlData.getToUserName(),
                System.currentTimeMillis() / 1000 + "", "text", returnContent);
    }
}
